package com.vanysoft.business.data.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.vanysoft.business.data.entity.CategoryEngineer;
import com.vanysoft.business.data.entity.CategoryList;
import com.vanysoft.business.data.service.CategoryEngineerService;
import com.vanysoft.business.sys.entity.UserInfo;
import com.vanysoft.framework.aop.VanySoftValidation;
import com.vanysoft.framework.config.mvc.SessionUser;
import com.vanysoft.framework.utils.JSONResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;

/**
 * <p>
 * 品类工程师信息表 前端控制器
 * </p>
 *
 * @author YangLei
 * @since 2018-07-09
 */
@Controller
@RequestMapping("/data/categoryEngineer")
public class CategoryEngineerController {

    @Autowired
    CategoryEngineerService categoryEngineerService;

    @ResponseBody
    @RequestMapping("list")
    public JSONResponse list(Page<CategoryList> page, String[] codes, String[] users, int status) {
        page = categoryEngineerService.selectPage(page, StringUtils.join(codes,","), StringUtils.join(users,","), status);
        return new JSONResponse(page);
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    @VanySoftValidation
    public JSONResponse save(@Valid CategoryEngineer categoryEngineer, BindingResult result, HttpServletRequest request, @SessionUser UserInfo userInfo) {
        if (StringUtils.isEmpty(categoryEngineer.getId())) {
            categoryEngineer.setCreateTime(new Date());
            categoryEngineer.setCreateUser(userInfo.getId());
            categoryEngineer.setModifyUser(userInfo.getId());
            categoryEngineer.setModifyTime(new Date());
            categoryEngineerService.insert(categoryEngineer);
        } else {
            categoryEngineer.setModifyUser(userInfo.getId());
            categoryEngineer.setModifyTime(new Date());
            categoryEngineerService.updateById(categoryEngineer);
        }
        return new JSONResponse();
    }
}

